﻿using System;
using System.Reflection; // 用于获取程序集路径
// using System.Windows.Media.Imaging; // 暂时不需要图标
using Autodesk.Revit.UI; // UI相关的API
// using Autodesk.Revit.DB; // 此文件可能不需要直接引用DB
using RevitApiExample1;
namespace RevitApiExample // 请确保与您项目的命名空间一致
{
    /// <summary>
    /// 实现 IExternalApplication 接口，在 Revit 启动时创建 Ribbon 面板
    /// </summary>
    public class SpaceTypeToolsApp : IExternalApplication
    {
        // 静态变量存储程序集路径，方便按钮使用
        static readonly string ExecutingAssemblyPath = Assembly.GetExecutingAssembly().Location;

        public Result OnStartup(UIControlledApplication application)
        {
            string tabName = "空间类型工具";
            string panelName = "类型管理与应用"; // 将所有功能放一个面板

            try
            {
                // 1. 创建 Ribbon Tab (选项卡)
                try
                {
                    application.CreateRibbonTab(tabName);
                }
                catch { /* Tab 可能已存在，忽略 */ }

                // 2. 创建 Ribbon Panel (面板)
                RibbonPanel ribbonPanel = application.CreateRibbonPanel(tabName, panelName);

                // 3. 创建按钮 - 定义与管理
                // --- 导入按钮 ---
                var importBtnData = new PushButtonData(
                    "ImportSpaceTypesBtn", "导入类型\n(CSV)", ExecutingAssemblyPath,
                    "RevitApiExample.ImportSpaceTypesCommand"); // 确保类名正确
                PushButton importBtn = ribbonPanel.AddItem(importBtnData) as PushButton;
                importBtn.ToolTip = "从 CSV 文件批量导入空间类型定义。";

                // --- 导出按钮 ---
                var exportBtnData = new PushButtonData(
                    "ExportSpaceTypesBtn", "导出类型\n(CSV)", ExecutingAssemblyPath,
                    "RevitApiExample.ExportSpaceTypesCommand"); // 确保类名正确
                PushButton exportBtn = ribbonPanel.AddItem(exportBtnData) as PushButton;
                exportBtn.ToolTip = "将项目中的空间类型定义导出到 CSV 文件。";

                // --- 创建按钮 ---
                var createBtnData = new PushButtonData(
                    "CreateSpaceTypeBtn", "创建新类型", ExecutingAssemblyPath,
                    "RevitApiExample.CreateSpaceTypeCommand"); // 确保类名正确
                PushButton createBtn = ribbonPanel.AddItem(createBtnData) as PushButton;
                createBtn.ToolTip = "通过界面创建一个新的空间类型。";

                // --- 编辑按钮 ---
                var editBtnData = new PushButtonData(
                    "EditSpaceTypeBtn", "编辑类型", ExecutingAssemblyPath,
                    "RevitApiExample.EditSpaceTypeCommand"); // 确保类名正确
                PushButton editBtn = ribbonPanel.AddItem(editBtnData) as PushButton;
                editBtn.ToolTip = "编辑选定的现有空间类型的属性。";

                // --- 删除按钮 ---
                var deleteBtnData = new PushButtonData(
                    "DeleteSpaceTypesBtn", "删除类型", ExecutingAssemblyPath,
                    "RevitApiExample.DeleteSpaceTypesCommand"); // 确保类名正确
                PushButton deleteBtn = ribbonPanel.AddItem(deleteBtnData) as PushButton;
                deleteBtn.ToolTip = "删除一个或多个未使用的空间类型。";

                ribbonPanel.AddSeparator(); // 添加分隔线

                // 4. 创建按钮 - 应用到空间
                // --- 修改单个空间按钮 ---
                var changeSingleBtnData = new PushButtonData(
                    "ChangeSingleSpaceTypeBtn", "修改单个空间", ExecutingAssemblyPath,
                    "RevitApiExample1.ChangeSpaceTypeCommand"); // 确保类名正确
                PushButton changeSingleBtn = ribbonPanel.AddItem(changeSingleBtnData) as PushButton;
                changeSingleBtn.ToolTip = "修改选定的单个空间的类型。";

                // --- 批量修改空间按钮 ---
                var batchChangeBtnData = new PushButtonData(
                   "BatchChangeSpaceTypeBtn", "批量修改空间", ExecutingAssemblyPath,
                   "RevitApiExample.BatchChangeSpaceTypeCommand"); // 确保类名正确
                PushButton batchChangeBtn = ribbonPanel.AddItem(batchChangeBtnData) as PushButton;
                batchChangeBtn.ToolTip = "批量修改选定的多个空间的类型。";

                ribbonPanel.AddSeparator(); // 添加分隔线

                
                return Result.Succeeded; // 启动成功
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Ribbon UI 错误", $"创建空间类型工具 Ribbon 时发生错误: {ex.Message}");
                return Result.Failed; // 启动失败
            }
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            // 在 Revit 关闭时执行的代码，通常返回成功即可
            return Result.Succeeded;
        }

    } // end class SpaceTypeToolsApp

} // end namespace